/*
Copyright (c) 2008, Oleg S. Estekhin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the copyright holders nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package ebml;

/** Contains the EBML element description. */
public final class ElementDescriptor {

    private final VInteger identifier;

    private final String name;

    private final ElementType type;


    /**
     * Creates a new <code>ElementDescriptor</code> object.
     *
     * @param identifierValue the element identifier value in the encoded form
     * @param name the element name or <code>null</code> if the name is not known
     * @param type the element type or <code>null</code> if the type is not known
     *
     * @throws IllegalArgumentException if <code>identifierValue</code> can not be decoded as an element identifier
     */
    public ElementDescriptor(long identifierValue, String name, ElementType type) {
        this(VInteger.decode(identifierValue), name, type);
    }

    /**
     * Creates a new <code>ElementDescriptor</code> object.
     *
     * @param identifier the element identifier
     * @param name the element name or <code>null</code> if the name is not known
     * @param type the element type or <code>null</code> if the type is not known
     *
     * @throws NullPointerException if <code>identifier</code> is <code>null</code>
     */
    public ElementDescriptor(VInteger identifier, String name, ElementType type) {
        if (identifier == null) {
            throw new NullPointerException("identifier is null");
        }
        this.identifier = identifier;
        this.name = name;
        this.type = type;
    }


    /**
     * Returns the element identifier.
     *
     * @return the element identifier in the encoded form
     */
    public VInteger getIdentifier() {
        return identifier;
    }

    /**
     * Returns the element identifier.
     *
     * @return the element identifier
     */
    public long getIdentifierValue() {
        return identifier.getEncodedValue();
    }

    /**
     * Returns the element name.
     *
     * @return the element name or <code>null</code> if the name is not known
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the element type.
     *
     * @return the element type or <code>null</code> if the type is not known
     */
    public ElementType getType() {
        return type;
    }


    @Override
    public int hashCode() {
        int result = 17;
        result = 37 * result + identifier.hashCode();
        result = 37 * result + (name == null ? 0 : name.hashCode());
        result = 37 * result + (type == null ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj instanceof ElementDescriptor) {
            ElementDescriptor descriptor = (ElementDescriptor) obj;
            return identifier.equals(descriptor.identifier)
                    && (name == null && descriptor.name == null || name != null && name.equals(descriptor.name))
                    && (type == null && descriptor.type == null || type != null && type.equals(descriptor.type));
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("ElementDescriptor(");
        buffer.append("identifier=").append(identifier);
        if (name != null) {
            buffer.append(',').append("name=").append(name);
        }
        if (type != null) {
            buffer.append(',').append("type=").append(type);
        }
        buffer.append(')');
        return buffer.toString();
    }

}
